import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../resources/colours.dart';
import '../../../utils/toast_utils.dart';
import '../../../utils/user.dart';
import '../../../utils/utils.dart';
import '../../../widgets/asset_item.dart';
import '../../../widgets/base/qkd_dialog.dart';
import '../../../widgets/base/qkd_text.dart';

typedef OnSend = void Function(TextEditingController textController,FocusNode inputFocus);

class CommentInput extends StatefulWidget {
  const CommentInput({
    super.key,
    this.onSend,
    this.isSecondary = false,
    this.append,
    this.focusNode,
    this.secondaryAt
  });

  final FocusNode? focusNode;
  final OnSend? onSend;
  final bool isSecondary;
  final String? secondaryAt;
  final List<Widget>? append;

  @override
  State<CommentInput> createState() => _CommentInputState();
}

class _CommentInputState extends State<CommentInput> {
  late FocusNode inputFocus = widget.focusNode ?? FocusNode();
  TextEditingController textController = TextEditingController();

  @override
  void initState() {
    inputFocus.addListener(() {
      if(inputFocus.hasFocus){
        setState(() {});
      }else{
        setState(() {});
      }
    });
    textController.addListener(() {

    });
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width,
      padding: const EdgeInsets.fromLTRB(16, 6, 0, 14),
      decoration: BoxDecoration(
        color: Colours.white,
        border: Border(
          top: BorderSide(
            color:inputFocus.hasFocus?Colours.greyEE:Colors.transparent,
            width: 0.5
          )
        )
      ),
      child: Row(
        crossAxisAlignment: inputFocus.hasFocus?CrossAxisAlignment.end:CrossAxisAlignment.center,
        children: [
          Expanded(
            child: CupertinoTextField(
              controller: textController,
              focusNode: inputFocus,
              decoration: BoxDecoration(
                  color: Colours.greyF7F9FC,
                  borderRadius: BorderRadius.circular(inputFocus.hasFocus?4:22)
              ),
              padding: const EdgeInsets.all(6),
              minLines: 1,
              maxLines: 3,
              // textInputAction: TextInputAction.next,
              // keyboardType: TextInputType.multiline,
              prefixMode: OverlayVisibilityMode.notEditing,
              prefix: widget.secondaryAt.hasValue?
                QkdText(
                  "@${widget.secondaryAt}",
                  color: Colours.grey66,
                  fontWeight: FontWeight.w500,
                ).paddingOnly(left: 12,right: 2):
                Image.asset(
                Utils.getImgPath("comment_pencil.png"),
                width: 14,
                height: 14,
              ).paddingOnly(left: 12,right: 2),
              placeholder: "友善评论",
              placeholderStyle: const TextStyle(color: Colours.grey99,fontSize: 14),
              style: const TextStyle(fontSize: 14,color: Colours.text),
              onTap: () async{
                if (User.isLogin) {
                  inputFocus.requestFocus();
                } else {
                  inputFocus.unfocus();
                  var result = await Get.dialog(
                      const QkdDialog.alert(
                          "登录后才可评论，要去登录吗？",
                          confirmText: "去登录"
                      )
                  );
                  if (result == true) {
                    await User.needLogin(() {});
                  }
                }
              },
              onTapOutside: (value) {
                if(value.position.dy < 485) {
                  inputFocus.unfocus();
                }
              },
              onChanged: (str) {
                if (str.length > 200) {
                  ToastUtils.show("发布内容不能多于200个字");
                  textController.text = str.substring(0,200);
                }else{
                  textController.text = str;
                }
                setState(() {});
              },
            ),
          ),
          if(inputFocus.hasFocus || widget.isSecondary)
            GestureDetector(
              behavior: HitTestBehavior.opaque,
              onTap: () => widget.onSend?.call(textController,inputFocus),
              child: QkdText('发布',
                  fontSize: 16,
                  fontWeight: textController.text.hasValue?FontWeight.w500:FontWeight.w400,
                  color: textController.text.hasValue?Colours.main:Colours.grey99
              ).paddingOnly(left: 10,right: 16,bottom: 5),
            )
          else...[
            Container(width: 16),
            ...widget.append??[],
            Container(width: 16)
          ]
        ],
      ),
    );
  }
}
